<template>
  <div class="login-container">
    <div class="title-container">
      <img src="@/assets/common/login.png" alt="">
    </div>

    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <h3 style="display: flex;">
        <img style="width:35px; height:30px; margin-right: 15px;" src="../../assets/common/dl.png" alt="">
        <span style="color:#000; font-size: 25px;font-weight: normal;">用户登录</span>
        <el-dropdown trigger="click" style="margin-left: 340px; font-size: 25px;" @command="handlerCommand">
          <span>
            <el-tooltip class="item" effect="dark" :content="$t('navbar.' + 'lang')" placement="bottom">
              <svg-icon icon-class="language" />
            </el-tooltip>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item :disabled="$i18n.locale === 'zh'" command="zh">中文</el-dropdown-item>
            <el-dropdown-item :disabled="$i18n.locale === 'en'" command="en">English</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </h3>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="请输入手机号码"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%; height: 55px; margin-bottom:30px; font-size: 20px;" @click.native.prevent="handleLogin">{{ $t('login.' + 'login') }}</el-button>

    </el-form>
  </div>
</template>

<script>
import { usernameRules } from '@/utils/validate'
import { mapActions } from 'vuex'
import Cookie from 'js-cookie'
export default {
  name: 'Login',

  data() {
    const userRules = (rule, value, callback) => {
    if (usernameRules(value)) {
      callback()
    } else {
      callback(new Error('请输入6-16位合法用户名'))
    }
  }
    return {
      login: 'login',
      loginForm: {
        username: 'root@admin.com',
        password: '123456'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '用户名不能为空' }, { trigger: 'blur', validator: userRules, message: '用户名格式不正确' }],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  // mounted() {
  //   this.$store.dispatch('user/userlogin', {
  //       mobile: '13800000002',
  //       password: '123456'
  //   })
  // },

  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handlerCommand(type) {
      if (type === 'zh') {
        Cookie.set('language', 'zh')
        this.$i18n.locale = Cookie.get('language')
        this.$message.success('切换多语言成功')
      } else {
        Cookie.set('language', 'en')
        this.$i18n.locale = Cookie.get('language')
        this.$message.success('切换多语言成功')
      }
     },
    ...mapActions(['user/userlogin']),
    async  handleLogin() {
      // 通过全部校验规则， 发送登录请求
    //  this.$refs.loginForm.validate(async (flag) => {
    //   if (flag) {
    //     try {
    //       await this['user/userlogin'](this.loginForm)
    //       this.$router.push('/')
    //     } catch (error) {
    //       console.log(error)
    //     }
    //   }
    //  })
      this.$refs.loginForm.validate(async isOK => {
        if (isOK) {
        //   const res = await login({
        //   username: 'root@admin.com',
        //   password: '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92'
        // })
        //   console.log(res)
          try {
            await this.$store.dispatch('user/userLogin', {
              username: this.loginForm.username,
              password: this.$sha256(this.loginForm.password)
            })
            this.$router.push('/')
          } catch (error) {
            console.log(error)
           }
        }
      })
      // const res = await axios({
      //   url: 'http://hmmm-api.itheima.net/frame/login',
      //   method: 'post',
      //   data: {
      //     username: 'root@admin.com',
      //     password: '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92'
      //   }
      // })
      // console.log(res)
      // console.log(this.$md5('123456'))
    }
  }
}
</script>

<style lang="scss">

/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #333;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: #fff;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  padding-left: 100px;
  padding-right: 400px;
  width: 100%;
  background: url(../../assets/common/bg.jpg) no-repeat center/cover;
  overflow: hidden;

  .login-form {
    position: relative;
    min-width: 600px;
    width: 600px;
    height: 350px;
    max-width: 100%;
    padding: 30px 35px 0;
    margin: 300px auto;
    overflow: hidden;
    background-color: #f5f5f5;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: absolute;
    width: 700px;
    height: 460px;
    right: 0;
    bottom: 0;

   image{
    width: 700px;
    height: 460px;
   }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
